package dtnnews.supports;

import java.util.ArrayList;
import java.util.List;

import com.example.dtnnews.R;

import dtnnews.models.News;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListNewsAdapter extends BaseAdapter {

	private ArrayList<News> lstNews;
	Context ctxt;
	LayoutInflater myInflater;
	
	public ListNewsAdapter(ArrayList<News> lstNews, Context ctxt) {
		this.lstNews = lstNews;
		this.ctxt = ctxt;
		myInflater = (LayoutInflater) ctxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public int getCount() {
		
		return lstNews.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return lstNews.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if(convertView == null) {
			convertView =  myInflater.inflate(R.layout.row, parent, false);
		}
		NewsHolder holder = new NewsHolder(convertView);
		holder.populateFrom(lstNews.get(position));
		return (convertView);
	}
	
	
	static class NewsHolder {
		private TextView title = null;
		private TextView content = null;
		private ImageView icon = null;
		private View row = null;
		
		NewsHolder (View row) {
			this.row = row;
			
			title = (TextView) row.findViewById(R.id.row_title);
			content = (TextView) row.findViewById(R.id.row_content);
			icon = (ImageView) row.findViewById(R.id.row_icon);
		} 
		
		public void populateFrom(News news) {
			title.setText(news.getTitle());
			content.setText(news.getContent());
			Tools.LoadImage(icon, news.getIconUrl());
		}
		
		
		
	}

}


